<?php

require_once 'db.inc';
require_once 'params.inc';
require_once 'mail.inc';
require_once 'settings.inc';

define ('SUBSCRIPTION_NOT_CONFIRMED', 1);
define ('SUBSCRIPTION_CONFIRMED', 2);


class Subsciption
{
	static public function add($email)
	{
		$email = trim($email);
		
		$confirmed = false;
		if (!($id=self::exist($email,$confirmed)) && Mail::validate($email))
		{
			global $db;
			$q = createQuery("
				INSERT INTO cf_subscribers	(email, created, state_id)
				VALUES ('$email', NOW(), ".SUBSCRIPTION_NOT_CONFIRMED.")
			");
			$q->execute();
			$id = $db->lastInsertId();
		}		
		if (!$confirmed)
		{
			self::sendConfirm($email,$id);
		}
	}
	
	static public function confirm($id)
	{
		$q = createQuery("
			UPDATE cf_subscribers 
			SET state_id = ".SUBSCRIPTION_CONFIRMED."
			WHERE id = $id
		");
		$q->execute();
		
		return $q->processedCount();
	}
	
	static public function delete($id)
	{
		$q = createQuery("
			DELETE FROM cf_subscribers
			WHERE id=$id
		");
		$q->execute();
		$q->close();
	}
	
	static public function send($subject, $body)
	{
		global $settings;
		
		static $emails = false;
		if (!$emails) 
		{
			$emails = array();
			
			$q = createQuery("
				SELECT email 
				FROM cf_subscribers
				WHERE state_id = ".SUBSCRIPTION_CONFIRMED."
			");
			$q->execute();
			while ($q->fetch())
			{
				$emails[] = $q->at(0);
			}
		}
		
		
		$m = new Mail($subject, $body);
		$m->setTypeHTML();
		$m->setFrom($settings->admin_email);
		
		foreach ($emails as $email)
		{
			Mail::send($email, $m);
		}
	}
	

	static public function sendAll()
	{	
		$subs = createQuery("
			SELECT id,subject,txt 
			FROM cf_subscription_issues 
			WHERE send_at IS NOT NULL AND send_at < NOW()
		");
		
		$subs->execute();
		while ($subs->fetch())
		{
			$id = $subs->at(0);
			execQuery("UPDATE cf_subscription_issues SET send_at=NULL WHERE id=$id");
			$subject = $subs->at(1);
			$text = $subs->at(2);
			
			self::send($subject, $text);
		}
	}
	
	
	static private function exist($email, &$confirmed)
	{
		$q = createQuery("
			SELECT id,state_id
			FROM cf_subscribers
			WHERE email='$email'
		");
		$q->execute();
		if ($q->fetch()) 
		{
			$confirmed = ($q->at(1) == SUBSCRIPTION_CONFIRMED);
			return $q->at(0);
		}
		$confirmed = false;
		return 0;
	}
	
	static private function sendConfirm($email,$id)
	{
		global $settings;
	
		$subject = 'You have subscribed to news on ' . $_SERVER['SERVER_NAME'];
		
		$confirmLink = self::getOpLink() . 'do=confirm&id=' . $id;
		$delLink = self::getOpLink() . 'do=delete&id=' . $id;
		$body = 
			'Hello!' . '<br>' .
			$subject . '<br>' . 
			'To confirm your email address go:' .
			"<a href='$confirmLink'>$confirmLink</a>" . '<br>' .
			'or to stop receiving letters go: ' .
			"<a href='$delLink'>$delLink</a>";
			
		$m = new Mail($subject, $body);
		$m->setTypeHTML();
		$m->setFrom($settings->admin_email);
		
		Mail::send($email, $m);
	}
	
	static private function getOpLink()
	{
		global $par;
		
		return 
			substr(
				$par->getPageLink(),
				0,
				strrpos($par->getPageLink(), '/')+1
			) . 
			'subscribe.php?';
	}
}



?>